/**
 * Created by gakuin on 5/18/16.
 */
import fetch from 'isomorphic-fetch'
import {actionCreator} from '../utils/creator'
import {fetchGet,fetchPost} from '../utils/httpRequest'
import * as types from '../constance/actionType.js'
import cookie from 'react-cookie'
const receiveBannerList = actionCreator(types.RECEIVE_BANNER_LIST, 'data','cates');
const needFetchData = actionCreator(types.NEED_FETCH_DATA, 'needFetch');
const receiveValueCardListList = actionCreator(types.RECEIVE_VALUE_CARD_LIST, 'data', 'page');
const receiveCateCardListList = actionCreator(types.RECEIVE_CATE_CARD_LIST, 'data', 'page','cate_id');

const receiveMyCardListList = actionCreator(types.RECEIVE_MY_CARD_LIST, 'data');
const receiveValueCardDetail = actionCreator(types.RECEIVE_VALUE_CARD_DETAIL, 'data');
const receiveGetOrderAndPay = actionCreator(types.RECEIVE_GET_ORDER_AND_PAY, 'data', 'payend');

const receiveMyUnUsedCardList = actionCreator(types.RECEIVE_MY_UNUSED_CARD_LIST, 'data');
const receiveMyNotPayCardList = actionCreator(types.RECEIVE_MY_NOTPAY_CARD_LIST, 'data');
const receiveUsedCardList = actionCreator(types.RECEIVE_MY_USED_CARD_LIST, 'data');
const receiveOverdueCardList = actionCreator(types.RECEIVE_MY_OVERDUE_CARD_LIST, 'data');


const receiveContinuePay = actionCreator(types.RECEIVE_CONTINUE_PAY, 'data');
const receiveCancelOrderResultCode = actionCreator(types.RECEIVE_CANCEL_ORDER, 'data');
const payend = actionCreator(types.PAY_END, 'data');
const cleanPayend = actionCreator(types.CLEAN_PAY_END, 'data');
const receiveStoreList = actionCreator(types.RECEIVE_STORE_LIST, 'data');
const receiveRegister = actionCreator(types.RECEIVR_REGISTER, 'data');
const refreshData = actionCreator(types.REFRESH_CARD_DATA, 'data');
//const receiveChangeCate= actionCreator(types.CHANGE_CATE, 'cate_id');//分类切换

//获取轮播广告列表
export function bannerListFetch(department_id) {

    if(!sessionStorage.getItem("index_bannerCate"+department_id)){
        return (dispatch, getState) => {
            let para = {department_id:department_id};
            return fetchGet('getBanner', para)
                .then(response =>response.json())
                .then(json => {
                    //console.log(json);
                    sessionStorage.setItem("index_bannerCate"+department_id,JSON.stringify(json));
                    dispatch(receiveBannerList(json.data,json.cates));
                })
        }
    }else{
        var mydata=JSON.parse(sessionStorage.getItem("index_bannerCate"+department_id));

        return (dispatch, getState) => {
            dispatch(receiveBannerList(mydata.data,mydata.cates));
        }
    }
}
//获取首页超值抢卡券列表
export function vauleCardListFetch(page = 1,department_id='',cate_id='') {
    return (dispatch, getState) => {
        $('#loadingToast').show();

        let para = {page:page,department_id:department_id};

        return fetchGet('cardlist', para)
            .then(response => response.json())
            .then(json => {
                $('#loadingToast').hide();

                dispatch(receiveValueCardListList(json.data, page));
            })
    }
}
//分类卡券列表
export function cateCardListFetch(page = 1,department_id=0,cate_id=0) {
    return (dispatch, getState) => {
        $('#loadingToast').show();

        let para = {page:page,department_id:department_id};
        //分类ID
        if(cate_id){
            para.cate_id=cate_id;
        }
        return fetchGet('cardlist', para)
            .then(response => response.json())
            .then(json => {
                $('#loadingToast').hide();

                dispatch(receiveCateCardListList(json.data, page,cate_id));
            })
    }
}
export function valueCardDetailFetch(id) {
    $('#loadingToast').show();
    return (dispatch, getState) => {
        let para = {id};
        return fetchGet('cardDetail', para)
            .then(response => response.json())
            .then(json => {
                if(json.data&&json.data.card_rule){
                    json.data.card_rule=json.data.card_rule.replace(/\n/g,"<br/>");
                }
                dispatch(receiveValueCardDetail(json.data));
            })
    }
}

//获取我的卡券列表
export function myCardListFetch(uid) {
    return (dispatch, getState) => {
        let para = {uid};
        return fetchGet('myCardList', para)
            .then(response => response.json())
            .then(json => {
                dispatch(receiveMyCardListList(json.data));
            })
    }
}

//立即抢购
export function getOrderAndPayFetch(uid, card_id, card_number) {
    return (dispatch, getState) => {
        let para = {uid, card_id, card_number};
        dispatch(payend(false));
        return fetchPost('pay', para)
            .then(response => response.json())
            .then(json => {
                //  {data:json,fetchEnd:true}
                dispatch(receiveGetOrderAndPay(json, true));
            })
    }
}

export function cleanPayEnd() {
    return (dispatch, getState) => {
        dispatch(cleanPayend(false));
    }
}

//我的卡券列表-已支付
export function getMyUnusedListFetch(uid, status) {
    $('#loadingToast').show();
    return (dispatch, getState) => {
        let para = {uid, status,o2osign:cookie.load('o2osign')};
        return fetchGet('myCardList', para)
            .then(response => response.json())
            .then(json => {
                //if(window.now_index==1)
                //{
                    dispatch(receiveMyUnUsedCardList(json));
                //}
            })
    }
}
//清除我的卡券列表-已支付
//export function cleanMyUnusedListFetch() {
//    return (dispatch, getState) => {
//        dispatch(receiveMyUnUsedCardList({code: -1}));
//    }
//}
//我的卡券列表-未支付
export function getMyNotPayListFetch(uid, status) {
    $('#loadingToast').show();
    return (dispatch, getState) => {
        let para = {uid, status,o2osign:cookie.load('o2osign')};
        return fetchGet('myCardList', para)
            .then(response => response.json())
            .then(json => {
                //if(window.now_index==0){//当前如果已不再未选中  直接不触发事件
                    dispatch(receiveMyNotPayCardList(json));
                //}
            })
    }
}

//清除我的卡券列表-未支付
//export function cleanMyNotPayListFetch() {
//    return (dispatch, getState) => {
//        dispatch(receiveMyNotPayCardList({code: -1}));
//    }
//}
//我的卡券列表-已使用
export function getMyUsedCardsFetch(uid, status) {
    $('#loadingToast').show();
    return (dispatch, getState) => {
        let para = {uid, status,o2osign:cookie.load('o2osign')};
        return fetchGet('myCardList', para)
            .then(response => response.json())
            .then(json => {
                //if(window.now_index==2)
                //{
                    dispatch(receiveUsedCardList(json));
                //}

            })
    }
}

//清除我的卡券列表-已使用
//export function cleanMyUsedCardsFetch() {
//    return (dispatch, getState) => {
//        dispatch(receiveUsedCardList({code: -1}));
//    }
//}
//我的卡券列表-已过期
export function getMyOverDueCardsFetch(uid, status) {
    $('#loadingToast').show();
    return (dispatch, getState) => {
        let para = {uid, status,o2osign:cookie.load('o2osign')};
        return fetchGet('myCardList', para)
            .then(response => response.json())
            .then(json => {
                //if(window.now_index == 3)
                //{
                    dispatch(receiveOverdueCardList(json));
                //}
            })
    }
}
//清除我的卡券列表-已过期
//export function cleanMyOverDueCardsFetch() {
//    return (dispatch, getState) => {
//        dispatch(receiveOverdueCardList({code: -1}));
//    }
//}
//取消订单
export function cancelOrderFormFetch(orderform_id) {
    return (dispatch, getState) => {
        let para = {orderform_id:orderform_id,o2osign:cookie.load('o2osign'),mobile:cookie.load('mobile')};

        return fetchPost('cancelOrderForm', para)
            .then(response => response.json())
            .then(json => {
                 dispatch(receiveCancelOrderResultCode({code: json.code}));
            })
    }
}
//清除取消订单
export function cleanCancelOrderForm() {
    return (dispatch, getState) => {

        dispatch(receiveCancelOrderResultCode({code: -1}));

    }
}

//获取指定商户的门店列表
export function storeListFetch(business_id) {
    $('#loadingToast').show();
    return (dispatch, getState) => {
        let para = {business_id};
        return fetchGet('storeList', para)
            .then(response => response.json())
            .then(json => {
                //console.log(json);
                dispatch(receiveStoreList(json.data));
            })
    }

}

//export function changeCate(cate_id){
//    console.log("trigger change event");
//    return (dispatch, getState) => {
//            dispatch(receiveChangeCate({cate_id:cate_id}));
//    }
//}

export function frontPayBack(orderno,returnStatus){

    return (dispatch, getState) => {
        let para = {orderform_code:orderno,returnStatus:returnStatus};
        return fetchGet('getCityOrder', para)
    }
}
